<?php

declare (strict_types = 1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://doc.hyperf.io
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
 */

namespace App\Controller;

use App\Model\User;
use Hyperf\Di\Annotation\Inject;
use Phper666\JwtAuth\Jwt;
use Tbqphplib\Hyperfbase\AbstractController;

class IndexController extends AbstractController
{
    /**
     * @Inject
     *
     * @var Jwt
     */
    protected $jwt;

    public function index()
    {
        $user = $this->request->input('user', 'Hyperf');
        $method = $this->request->getMethod();

        return [
            'method' => $method,
            'message' => "Hello {$user}.",
        ];
    }

    public function login()
    {
        $user = User::query()->where('account', $this->request->input('account'))->first();

        //验证用户账户密码
        if (!empty($user->password) && password_verify($this->request->input('password'), $user->password)) {
            $userData = [
                'id' => $user->id,
                'account' => $user->account,
            ];

            //更新用户登录时间
            User::query()->where('account', $this->request->input('account'))->update([
                'last_time' => date('Y-m-d H:i:s'),
            ]);

            $token = $this->jwt->getToken($userData);
            $data = [
                'token' => (string) $token,
                'exp' => $this->jwt->getTTL(),
            ];

            return $this->success($data);
        }

        return $this->failed(100100);
    }

    public function regist()
    {
        $userData = [
            'account' => $this->request->input('account'),
            'password' => password_hash($this->request->input('password'), PASSWORD_DEFAULT),
        ];

        $User = User::query()->where('account', $userData['account'])->first();

        if (!empty($User->account)) {
            return $this->failed(100000);
        }

        $user = User::query()->firstOrCreate($userData);
        return $this->success($user);
    }

    public function logout()
    {
        $data = ['data' => $this->jwt->logout()];
        return $this->success($data);
    }

    public function refreshToken()
    {
        $newToken = $this->jwt->refreshToken();
        return $this->success(['newToken' => $newToken]);
    }

    public function getUserInfoWithToken()
    {
        //获取token中的用户数据
        $user = $this->jwt->getParserData();

        $userInfo = User::query()->where('account', $user['account'])->get();

        $data = $userInfo->map(function ($userInfo) {
            return [
                'id' => $userInfo->id,
                'account' => $userInfo->account,
                'created_at' => $userInfo->created_at->toDateTimeString(),
                'roles' => ['admin'],
            ];
        });

        return $this->success($data[0]);
    }

    public function getUserInfoById()
    {
        return [];
    }

    public function deleteUser()
    {
        return [];
    }

    public function updateUser()
    {
        return [];
    }

    public function addUser()
    {
        return [];
    }

    public function userList()
    {
        return [];
    }
}
